package com.xx.springboot.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.xx.springboot.entity.ResponseBo;
import com.xx.springboot.entity.User;
import com.xx.springboot.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxiunique
 * @since 2018-08-06
 */
@RestController
@RequestMapping("/user")
@RequiresRoles("superAdmin")
public class UserController extends ApiController {

    @Autowired
    private IUserService userService;

    /**
     * 查询用户列表
     * @return
     */
    @RequestMapping("/list")
    @RequiresRoles("superAdmin")
    public ResponseBo getUserList(Integer pageNo, Integer pageSize){
        if(pageNo == null || pageSize == null || pageNo < 0 || pageSize <= 0)
            return ResponseBo.error("请输入正确的分页条件!");

        return userService.getUserList(pageNo, pageSize);
    }

    /**
     * <p>
     *     添加用户
     * </p>
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public ResponseBo addUser(@Valid User user) {
        try {
            userService.save(user);
            return ResponseBo.ok(user);
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    /**
     * <p>
     *     修改用户
     * </p>
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public ResponseBo udpUser(User user) {
        if (user == null || user.getId() < 0)
            return ResponseBo.error("请传递正确的参数");
        try {
            userService.updateById(user);
            return ResponseBo.ok("修改成功");
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }


    /**
     * <p>
     *     修改用户状态
     * </p>
     * @param userId
     * @return
     */
    @RequestMapping("remove")
    public ResponseBo removeUser( Long userId) {
        if (userId == null || userId < 0)
            return ResponseBo.error("请传递正确的参数");
        User user = new User();
        user.setId(userId);
        user.setStatus(1);
        try {
            userService.updateById(user);
            return ResponseBo.ok();
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }


}

